<?php
class ss_dao_category extends ss_dao_object{
	public function master(){
		return array('name'=>$this->name(),'slug'=>$this->slug(),'entries'=>$this->entrysluglist(),'$ref'=>$this->primary());
	}
	public function detail(){
		return $this->getData();
	}
	
	public function entrysluglist(){
		if(is_null($this->entrysluglist)) return array();
		$entries=explode(',',$this->entrysluglist);
		foreach($entries as $slug){
			$result[$slug]=array('$ref'=>'/entry/'.$slug);
		}
		return $result;
	}
}

class ss_dao_category_mapper extends ss_dao_mapper{
	protected $table='category';
	protected $primary='idcategory';
	
	private function getArray($stmt){
		$result=array();
		while($obj=$stmt->fetchObject('ss_dao_category',array($this->primary))){
			$result[]=$obj->clean();
		}
		$this->da->closeCursor($stmt);
		return $result;
	}
	
	public function api_master($query=null){
		$q=$this->injector->create('ss_dao_query');
		$sql=$q
		->from(array(
			'categorywithentries'))
		->where($query)
		->select();
		$stmt=$this->da->runStmt($sql,$q->values());
		return $this->getArray($stmt);
	}
	
	public function api_detail($id){
		$q=$this->injector->create('ss_dao_query');
		$sql=$q
		->from(array(
			'categorywithentries'))
		->where(array(
			array($this->primary=>$id))
			)
		->select();
		$stmt=$this->da->runStmt($sql,$q->values());
		return $stmt->fetchObject('ss_dao_category',array($this->primary));
	}
	
	public function query($query){
		return $this->master();
	}
}
?>